package OrderService.controller;


import OrderService.IService.IOrderService;
import OrderService.mapper.OrderMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import pojo.Order;
import pojo.User;
import api.UserServiceApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags="订单接口")
@RestController
@RequestMapping(value="/order")
@RequiredArgsConstructor

public class OrderController {

    private final IOrderService orderService;
//    private final RestTemplate restTemplate;

    @Autowired
    private UserServiceApi userServiceApi;
//    @Autowired
//    private OrderMapper orderMapper;

    @ApiOperation("OrderID查询用户信息以及订单信息")
    @GetMapping("/{order_userid}")
    public Order getorder(@ApiParam("OrderID") @PathVariable int order_userid) {
        Order order = orderService.getById(order_userid);
//RestTemplate调取服务

//        String url = "http://UserService/user/" + order.getUserid();
//
//        User user = restTemplate.getForObject(url, User.class);
//feign 远程调用 内部自带负载均衡
        System.out.println(order.getOrder_userid());
        User user = userServiceApi.getUser(order.getOrder_userid());
        order.setUser(user);

        return order;
    }
    @ApiOperation("分页查询所有订单")
    @GetMapping("/selectAll")
    @ResponseBody
    public List<Order> selectALLOrder(@ApiParam("当前页")@RequestParam int page,@ApiParam("页面大小数量")int num){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>(); //查询默认大于等于0
        queryWrapper.ge("orderid",0); //设定orderid都大于等于0所以是全搜索
        Page<Order> orderPage = new Page<>(page,num);//查询当前page一共num页
        orderPage = orderService.page(orderPage,queryWrapper); //查询orderpage
        //输出当页情况
        System.out.println("总记录数：" + orderPage.getTotal());
        System.out.println("总共多少页：" + orderPage.getPages());
        System.out.println("当前页码：" + orderPage.getCurrent());
        // 当前页数据
        return orderPage.getRecords();
    }

}
